package com.yachang.qym.usbhost.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Handler;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.snappydb.DB;
import com.snappydb.SnappydbException;
import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.UsbApplication;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by qym on 2015/8/17.
 */
public class LoadImage {

    public static byte[] Bitmap2Bytes(Bitmap bm){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public static Bitmap getPicFromBytes(byte[] bytes,
                                         BitmapFactory.Options opts) {
        if (bytes != null)
            if (opts != null)
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
                        opts);
            else
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        return null;
    }

    public static void showImage(String imageUrl, final ImageView imageView) {
        DB db = UsbApplication.getDB();
        try {
            byte[] imageByte = db.getBytes(imageUrl);
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = false;
            Bitmap bitmap = getPicFromBytes(imageByte, opts);
            imageView.setImageBitmap(bitmap);
        } catch (SnappydbException e) {
            e.printStackTrace();
            Handler handler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    Bitmap bitmap = (Bitmap) msg.obj;
                    imageView.setImageBitmap(bitmap);
                    super.handleMessage(msg);
                }
            };
            GetBitmapThread thread = new GetBitmapThread(imageUrl, handler);
            thread.start();
        }
    }

    static class GetBitmapThread extends Thread{

        private String url;
        private Handler handler;

        public GetBitmapThread(String url, Handler handler){
            this.url = url;
            this.handler = handler;
        }
        @Override
        public void run() {
            super.run();

            DB db = UsbApplication.getDB();
            Bitmap bitmap = BitmapFactory.decodeResource(UsbApplication.getContext().getResources(), R.mipmap.un_load);
            try {
                URL myFileUrl = null;
                myFileUrl = new URL(url);
                HttpURLConnection conn;
                conn = (HttpURLConnection) myFileUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();
                bitmap = BitmapFactory.decodeStream(is);
                Message message = new Message();
                message.obj = bitmap;
                byte[] imageBytes = Bitmap2Bytes(bitmap);
                db.put(url, imageBytes);
                handler.sendMessage(message);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (SnappydbException e) {
                e.printStackTrace();
            }
        }
    }

    public static Bitmap getBitmap(String imageUrl) {
        Bitmap bitmap = BitmapFactory.decodeResource(UsbApplication.getContext().getResources(), R.mipmap.un_load);
        DB db = UsbApplication.getDB();
        try {
            byte[] imageByte = db.getBytes(imageUrl);
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = false;
            bitmap = getPicFromBytes(imageByte, opts);
            return bitmap;
        } catch (SnappydbException e) {
            e.printStackTrace();
            URL myFileUrl = null;
            try {
                myFileUrl = new URL(imageUrl);
                HttpURLConnection conn;
                conn = (HttpURLConnection) myFileUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();
                bitmap = BitmapFactory.decodeStream(is);
                byte[] imageBytes = Bitmap2Bytes(bitmap);
                db.put(imageUrl, imageBytes);
            } catch (MalformedURLException e3) {
                e3.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            } catch (SnappydbException e1) {
                e1.printStackTrace();
            }
            return bitmap;
        }
    }

    public static Bitmap bigBitmap(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(3f,3f); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        return resizeBmp;
    }

    public static Bitmap _8p(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(0.8f,0.8f); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        return resizeBmp;
    }

    public static Bitmap zoomImage(Bitmap bitmap, double newWidth,
                                   double newHeight) {
        // 获取这个图片的宽和高
        float width = bitmap.getWidth();
        float height = bitmap.getHeight();
        // 创建操作图片用的matrix对象
        Matrix matrix = new Matrix();
        // 计算宽高缩放率
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 缩放图片动作
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, (int) width,
                (int) height, matrix, true);
        return newBitmap;
    }

    public static Bitmap smallBitmap(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(0.5f,0.5f); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        return resizeBmp;
    }

    public static Bitmap littleBitmap(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(0.5f,0.5f); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        return resizeBmp;
    }
}
